import yz_uploader from "components/ui_components/yz_uploader";
import { Toast } from "vant";
import district from "../../../public/static/app/gov_province_city_area_id"
export default {
  data() {
    return {
      district: district,
      checked: true,
      sexShow: false,
      showAdd: false,
      // 1债权人 2债务人 3VIP
      member_type: 1,
      // 身份证号
      identity_number: "",
      // 身份正面图片
      identity_front_image: "",
      // 身份反面图片
      identity_back_image: "",
      // 姓名，创建VIP不传
      name: "",
      // 1男 2女，创建VIP不传
      sex: "1",
      // 常住省份ID，创建VIP不传
      live_province_id: "",
      // 常住市ID，创建VIP不传
      live_city_id: "",
      // 常住区ID，创建VIP不传
      live_district_id: "",
      // 常住街道ID，创建VIP不传
      live_street_id: "",
      // 常住地址，创建VIP不传
      live_address: "",
      // 户籍省份ID，创建VIP不传
      source_province_id: "",
      // 户籍市ID
      source_city_id: "",
      // 户籍区ID，创建VIP不传
      source_district_id: "",
      // 户籍街道ID，创建VIP不传
      source_street_id: "",
      // 户籍地址，创建VIP不传
      source_address: "",
      // 和解协议图片数组json化字符串，创建VIP不传
      agreement_pics: "",
      // 补充材料图片数组json化字符串，创建VIP不传
      another_pics: "",
      // 营业执照图片数组json化字符串，创建债权人才传
      business_pics: "",

      showAddTwo: false,
      // 债务人会员ID，创建债权人时必传
      debtor_uid: "",
      // 常驻区域名
      addressOne: "",
      // 户籍区域名
      addressTwo: "",
      // 户籍街道开关
      streetShowtwo: false,
      // 户籍街道
      streettwo: "",
      //街道开关
      streetShow: false,
      street: "",
      districtVal: [],
      // 身份正面显示图片
      identity_front_image_show: "",
      // 身份反面显示图片
      identity_back_image_show: "",
      settingInfo: [],
      // 协议弹窗
      descShow: false,
      Vip_uid: [],
      showUserC: [],
      isStyle: {
        width: "5.25rem",
        height: "5.25rem",
        background: "#fff",
        border: "none",
      },
      introShow:false,
    };
  },
  activated() {
    this.member_type = this.$route.params.type;
    this.fun.setWXTitle(
      this.member_type == 1
        ? "债权人申请"
        : this.member_type == 2
        ? "债务人申请"
        : "VIP申请"
    );
    if (this.member_type == 3) {
      this.getVipUser();
    }
    this.getData();
  },
  mounted() {
  },
  watch:{},
  methods: {
    closePhoto(key){
      if(key == 1){
        this.$refs.imgz.removeImg(0);
        this.identity_front_image_show = "";
        this.identity_front_image = "";
      }else{
        this.$refs.imgf.removeImg(0);
        this.identity_back_image_show = "";
        this.identity_back_image = "";
      }
      
    },
    checkPost() {
      if (this.member_type == 1 || this.member_type == 2) {
        if (this.fun.isTextEmpty(this.name)) {
          Toast("请输入姓名");
          return false;
        }
        if (this.fun.isTextEmpty(this.sex)) {
          Toast("请选择性别");
          return false;
        }
        if (
          this.fun.isTextEmpty(this.live_province_id) ||
          this.fun.isTextEmpty(this.live_city_id) ||
          this.fun.isTextEmpty(this.live_district_id)
        ) {
          Toast("请选择常住区域");
          return false;
        }
        if (
          this.fun.isTextEmpty(this.source_province_id) ||
          this.fun.isTextEmpty(this.source_city_id) ||
          this.fun.isTextEmpty(this.source_district_id)
        ) {
          Toast("请选择户籍区域");
          return false;
        }
        if (this.fun.isTextEmpty(this.agreement_pics)) {
          Toast("请上传和解协议");
          return false;
        }
        if (this.fun.isTextEmpty(this.another_pics)) {
          Toast("请上传补充资料");
          return false;
        }
        if (this.settingInfo.is_street) {
          // 是否开启了街道
          if (this.fun.isTextEmpty(this.live_street_id)) {
            Toast("请选择常住街道");
            return false;
          }
          if (this.fun.isTextEmpty(this.source_street_id)) {
            Toast("请选择户籍街道");
            return false;
          }
        }
      }
      if (this.member_type == 2) {
        // 营业执照只有债务人才传
        if (this.fun.isTextEmpty(this.business_pics)) {
          Toast("请上传营业执照");
          return false;
        }
      }
      if (this.member_type == 1) {
        // 只有债权人才传
        if (this.fun.isTextEmpty(this.debtor_uid)) {
          Toast("请输入债务人会员ID");
          return false;
        }
      }

      if (this.fun.isTextEmpty(this.identity_number)) {
        Toast("请输入身份证号码");
        return false;
      }
      if (this.fun.isTextEmpty(this.identity_front_image)) {
        Toast("请上传身份证图片");
        return false;
      }
      if (this.fun.isTextEmpty(this.identity_back_image)) {
        Toast("请上传身份证图片");
        return false;
      }
      if(!this.fun.isTextEmpty(this.settingInfo.apply_desc)){
        if (!this.checked) {
          Toast("请阅读勾选申请协议");
            return false;
        }
      }
      return true;
    },
    getVipUser() {
      $http
        .get(
          "plugin.debt-shop.frontend.debt-member.memberParentInfo",
          {},
          "加载中"
        )
        .then((res) => {
          if (res.result === 1) {
            this.Vip_uid = res.data;
          } else {
            Toast(res.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    searchInp() {
      $http
        .get("member.member.memberInfo", { uid: this.debtor_uid }, "加载中")
        .then((res) => {
          if (res.result === 1) {
            this.showUserC = res.data;
          } else {
            Toast(res.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    streetConfirm(item, key) {
      console.log(item, key);
      if (key == "streetShowtwo") {
        console.log("aaaaaaa");
        this.streettwo = item.areaname;
        this.source_street_id = item.id;
        this.streetShowtwo = false;
      } else {
        this.street = item.areaname;
        this.live_street_id = item.id;
        this.streetShow = false;
      }
    },
    // 获取街道
    getStreet(param) {
      $http
        .get("member.memberAddress.street", { district_id: param })
        .then((response) => {
          if (response.result === 1) {
            if (
              !this.fun.isTextEmpty(response.data) &&
              response.data.length > 0
            ) {
              this.districtVal = response.data;
              this.strShow = true;
            } else {
              this.strShow = false;
            }
          } else {
            this.strShow = false;
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 城市结果
    resultAdd(data) {
      this.addressOne = `${data.itemName1}${data.itemName2}${data.itemName3}`;
      this.live_province_id = data.itemValue1;
      this.live_city_id = data.itemValue2;
      this.live_district_id = data.itemValue3;
      this.getStreet(data.itemValue3);
      this.showAdd = false;
    },
    resultAdd2(data) {
      this.addressTwo = `${data.itemName1}${data.itemName2}${data.itemName3}`;
      this.source_province_id = data.itemValue1;
      this.source_city_id = data.itemValue2;
      this.source_district_id = data.itemValue3;
      this.getStreet(data.itemValue3);
      this.showAddTwo = false;
    },
    selectSex(key) {
      this.sex = key;
      this.sexShow = false;
    },
    getIMganother_pics(e) {
      this.another_pics = e;
      console.log("补充材料", this.agreement_pics);
    },
    getIMgagreement(e) {
      this.agreement_pics = e;
      console.log("协议", this.agreement_pics);
    },
    getIMgbusiness_pics(e) {
      this.business_pics = e;
      console.log("营业执照", this.business_pics);
    },
    getIMg(e) {
      // 身份证正面 相对地址
      this.identity_front_image = e;
    },
    getIMgF(e) {
      // 身份证反面 相对地址
      this.identity_back_image = e;
    },
    getIDcardZ(e) {
      this.identity_front_image_show = e;
    },
    getIDcardF(e) {
      this.identity_back_image_show = e;
    },
    getData() {
      $http
        .post("plugin.debt-shop.frontend.debt-member.applyData",{member_type:this.member_type}, "正在获取")
        .then(
          (response) => {
            if (response.result == 1) {
              let data = response.data;
              this.settingInfo = data;
              if (this.member_type == 1) {
                if (data.owner_goods_count == 0) {
                  // 先去购买商品
                  this.$dialog
                    .confirm({
                      message: "请先购买指定商品",
                    })
                    .then(() => {
                      this.$router.push(
                        this.fun.getUrl("debtGood", { id: data.owner_goods_id })
                      );
                    })
                    .catch(() => {
                      this.$router.go(-1)
                    });
                  return;
                }
                if (data.is_vip) {
                  this.$dialog
                    .alert({ message: "您是VIP,暂时无法申请成为债务人" })
                    .then(() => {
                      // this.$router.push(this.fun.getUrl("debtindex", {}));
                      this.$router.go(-1);
                    });
                  return;
                }
              }
              if (this.member_type == 2) {
                if (data.debtor_goods_count == 0) {
                  // 先去购买商品
                  this.$dialog
                    .confirm({
                      message: "请先购买指定商品",
                    })
                    .then(() => {
                      this.$router.push(
                        this.fun.getUrl("debtGood", { id: data.debtor_goods_id })
                      );
                    })
                    .catch(() => {
                      this.$router.go(-1)
                    });
                  return;
                }
                if (data.is_vip) {
                  this.$dialog
                    .alert({ message: "您是VIP,暂时无法申请成为债务人" })
                    .then(() => {
                      this.$router.go(-1);
                    });
                  return;
                }
                if (data.is_supplier) {
                  this.$dialog
                    .alert({ message: "您是供应商,暂时无法申请成为债务人" })
                    .then(() => {
                      this.$router.go(-1);
                    });
                  return;
                }
                
              }
              if (this.member_type == 3) {
                if(data.last_apply_is_refund == 1){
                  this.$dialog
                    .alert({
                      message: "申请被驳回，请重新申请",
                    })
                }
                if (data.vip_goods_count == 0) {
                  // 如果为0先跳去商品详情购买
                  this.$dialog
                    .confirm({
                      message: "请先购买指定商品",
                    })
                    .then(() => {
                      this.$router.push(
                        this.fun.getUrl("debtGood", { id: data.vip_goods_id })
                      );
                    })
                    .catch(() => {
                      this.$router.go(-1)
                    });
                  return;
                }
                if (data.is_owner == 1 || data.is_debtor) {
                  this.$dialog
                    .alert({
                      message: "您无法申请成为vip",
                    })
                    .then(() => {
                      this.$router.go(-1);
                    });
                  return;
                }
              }
            } else {
              that.$dialog.alert({ message: response.msg });

              //Toast(response.msg);
            }
          },
          function (response) {
            // error callback
          }
        );
    },
    submit() {
      let check = this.checkPost();
      if (!check) {
        return;
      }
      let json = {
        member_type: this.member_type,
        identity_number: this.identity_number,
        identity_front_image: this.identity_front_image,
        identity_back_image: this.identity_back_image,
      };
      if (this.member_type == 1 || this.member_type == 2) {
        // 债权人传参
        json.name = this.name;
        json.sex = this.sex;
        json.live_province_id = this.live_province_id;
        json.live_city_id = this.live_city_id;
        json.live_city_id = this.live_city_id;
        json.live_district_id = this.live_district_id;
        json.live_street_id = this.live_street_id;
        json.live_address = this.live_address;
        json.source_province_id = this.source_province_id;
        json.source_city_id = this.source_city_id;
        json.source_district_id = this.source_district_id;
        json.source_street_id = this.source_street_id;
        json.source_address = this.source_address;
        json.agreement_pics = JSON.stringify(this.agreement_pics);
        json.another_pics = JSON.stringify(this.another_pics);
      }
      if (this.member_type == 2) {
        json.business_pics = JSON.stringify(this.business_pics);
      }
      if (this.member_type == 1) {
        // 债权人传参
        json.debtor_uid = this.debtor_uid;
      }
      $http.post("plugin.debt-shop.frontend.apply.apply", json, "正在获取").then(
        (response) => {
          if (response.result == 1) {
            console.log(response);
            this.$router.go(-1);
            if (this.member_type == 3) {
              that.$dialog
                .confirm({
                  message: "您已提交申请VIP，等待商家审核",
                })
                .then(() => {
                  this.$router.go(-1);
                })
                .catch(() => {});
            } else {
              this.$dialog
                .confirm({
                  message: "申请成功",
                })
                .then(() => {
                  this.$router.go(-1);
                })
                .catch(() => {});
            }
          } else {
            // that.$dialog.alert({ message: response.msg });

            Toast(response.msg);
          }
        },
        function (response) {
          // error callback
        }
      );
    },
  },
  components: { yz_uploader },
};
